/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.JavaFX.DragandDrop;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @author kylin
 */
public class TestDragandDrop extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Test Drag And Drop");

        Group root = new Group();
        Scene scene = new Scene(root, 400, 200);
        scene.setFill(Color.LIGHTGREEN);

        ImageView source = new ImageView(new Image(getClass().getResourceAsStream("drag.jpg")));
        source.setFitWidth(100);
        source.setFitHeight(100);

        ImageView target = new ImageView(new Image(getClass().getResourceAsStream("drop.png")));
        target.setFitWidth(100);
        target.setFitHeight(100);

        VBox box=new VBox();
        
        box.getChildren().add(source);
        box.getChildren().add(target);
        
        init(source, target);

        root.getChildren().add(box);
        
        stage.setScene(scene);
        stage.show();
    }

    public void init(ImageView source, ImageView target) {

        source.setOnDragDetected(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("检测到拖动操作");

                Dragboard db = source.startDragAndDrop(TransferMode.ANY);
                Image img = new Image(getClass().getResourceAsStream("drag.jpg"));
                double offsetx = img.getWidth() / 2;
                double offsety = img.getHeight() / 2;
                db.setDragView(img, offsetx, offsety);
                ClipboardContent content = new ClipboardContent();
                content.putImage(img);
                db.setContent(content);
                event.consume();
            }
        });

        target.setOnDragOver(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                //System.out.println("检测到拖动到目标位置");
                System.out.println(event.getDragboard().hasImage());
                if (event.getGestureSource() != target && event.getDragboard().hasImage()) {
                    event.acceptTransferModes(TransferMode.COPY);
                }
                event.consume();
            }
        });

        target.setOnDragEntered(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                if (event.getGestureSource() != target && event.getDragboard().hasString()) {
                    target.setEffect(null);
                }
                event.consume();
            }
        });

        target.setOnDragExited(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                target.setEffect(null);
                event.consume();
            }
        });

        target.setOnDragDropped(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                Dragboard db = event.getDragboard();
                boolean success = false;
                if (db.hasImage()) {
                    System.out.println(db.getImage());
                    target.setImage(db.getImage());
                    success = true;
                }
                event.setDropCompleted(success);
                event.consume();
            }
        });

        source.setOnDragDone(new EventHandler<DragEvent>() {
            public void handle(DragEvent event) {
                if (event.getTransferMode() == TransferMode.MOVE) {
                    //source.setText("");
                }

                event.consume();
            }
        });
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
